package com.topfinance.payment.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

import com.topfinance.cnaps2.ebo.ConfRouteRuleEbo;
import com.topfinance.frame.dao.HibernateEntityDao;
import com.topfinance.frame.exception.AppException;
import com.topfinance.payment.dto.TCfgRouteRuleDto;
import com.topfinance.payment.ebo.TCfgPortInEbo;
import com.topfinance.payment.ebo.TCfgPortOutEbo;
import com.topfinance.payment.ebo.TCfgRouteRuleEbo;
import com.topfinance.payment.service.ITCfgRouteRuleMgr;

/**
 * 该类自动实现了dao的一般方法，如add, remove, update
 * 在service中出现的大部分情况下是在向后台提交数据前的处理(如校验)及获取后台数据后的处理等
 * @author
 *
 */

public class TCfgRouteRuleMgrImpl extends HibernateEntityDao<TCfgRouteRuleEbo> implements ITCfgRouteRuleMgr {

	private static final Log log = LogFactory.getLog(TCfgRouteRuleMgrImpl.class);
	
	//获得路由规则
	public TCfgRouteRuleEbo getTCfgRouteRule(Integer oid) throws AppException{
		return (TCfgRouteRuleEbo)get(oid);
	}

	public void deleteTCfgRouteRule(Integer oid) throws AppException{
		removeById(TCfgRouteRuleEbo.class,oid);
	}

	//获得路由规则列表
	public List<TCfgRouteRuleEbo> getAllRouteRule() throws AppException{
		String hql = " from TCfgRouteRuleEbo t " ;
		String where = "";
		List<Object> paramList = new ArrayList<Object>();
		if (isNotBlank(where)) {
			where = " where " + where;
		}
		hql = hql + where;
		return find(hql,paramList.toArray());		
	}

	public TCfgRouteRuleEbo getRoute(String oid) throws AppException {
		return (TCfgRouteRuleEbo)get(oid);
	}

	public TCfgRouteRuleEbo addRoute(TCfgRouteRuleDto rt) throws AppException {
		TCfgPortOutEbo po = (TCfgPortOutEbo)get(TCfgPortOutEbo.class,rt.getOPID());
		TCfgRouteRuleEbo rr = new TCfgRouteRuleEbo();
		BeanUtils.copyProperties(rt, rr);
		rr.setTCfgPortOut(po);
		//rr.setInportEbo(pi);
		saveOrUpdate(rr);
		return rr;
	}

	public void deleteRoute(TCfgRouteRuleDto o) throws AppException {
		removeById(o.getClass(),o.getOid());
	}

	public void deleteRoute(Integer id) throws AppException {
		removeById(TCfgRouteRuleEbo.class,id);
	}

	public void editRoute(TCfgRouteRuleDto rt) throws AppException {
		TCfgPortOutEbo po = (TCfgPortOutEbo)get(TCfgPortOutEbo.class,rt.getOPID());
		TCfgRouteRuleEbo rr = (TCfgRouteRuleEbo)get(TCfgRouteRuleEbo.class,rt.getOid());
		BeanUtils.copyProperties(rt, rr);
		rr.setTCfgPortOut(po);
		update(rr);
	}
}
